package com.huibei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huibei.mapper.CityMapper;
import com.huibei.mapper.HouseMapper;
import com.huibei.pojo.House;
import com.huibei.service.IHouseService;
import com.huibei.util.ReturnData;
import com.huibei.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HouseController {

    @Autowired
    private IHouseService houseService;

    @Resource
    private HouseMapper houseMapper;

    @RequestMapping(value = "/getRooms",method = RequestMethod.POST)
    public Object getRoomsByWhere(@RequestParam Map<String, Object> map){
        System.out.println("条件酒店酒店");
        System.out.println(map);
        System.out.println(map);
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("HOUSE_CITY", map.get("cityName"));
        List<Map<String, Object>> list = houseService.listMaps(wrapper);

        System.out.println(list);
        if(list.size() > 0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS, list,"数据交互成功");
        }else{
            return new ReturnData(StatusCode.DATA_NULL, false, "数据交互失败");
        }
    }

    @RequestMapping(value = "/getRoomById", method = RequestMethod.POST)
    public Object getRoomById(@RequestParam Map<String, Object> map){
        System.out.println("房间信息ID为：" + map);
        System.out.println("房间信息ID为：" + map);
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("HOUSE_ID", map.get("houseId"));
        Map houseMap = houseService.getMap(wrapper);
        if(houseMap.containsKey("HOUSE_NAME")){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,houseMap,"房间信息查询成功");
        }else{
            return new ReturnData(StatusCode.DATA_NULL, false, "房间信息查询失败");
        }

    }

    //得到三亚的酒店信息
    @RequestMapping("/getCityHouse")
    public Object getHaiLanHouse(@RequestParam Map<String, Object> map){
        System.out.println("城市为：" + map);
        System.out.println("城市为：" + map);
        QueryWrapper<House> wrapper = new QueryWrapper<House>();
        wrapper.eq("HOUSE_CITY", map.get("cityName"));
        Page<House> page = new Page<>(Long.parseLong(map.get("pageNum") + ""), Long.parseLong( map.get("limit") + ""));
        houseMapper.selectPage(page, wrapper);
//        System.out.println("查询的数据为： "  );
//        page.getRecords().forEach(System.out::println);
//        System.out.println("当前页：" + page.getCurrent());
          List<House> houseList =  page.getRecords();
          Map<String, Object> map1 = new HashMap<>();
          map1.put("houseList", houseList);
          map1.put("total", page.getTotal());
        /*注意  2个分页的必要参数   page limit*/
       // List<Map<String, Object>> houseList = service.selectCityHouseByName(map);
       // PageInfo<Map<String,Object>> pageInfo=new PageInfo<Map<String,Object>>(houseList);
        //map.put("houseList",houseList);
        /*第三个参数  为查询list的聚合最大值*/
       // map.put("total",pageInfo.getTotal());
        return new ReturnData(StatusCode.REQUEST_SUCCESS, map1,"城市酒店信息查询成功！");

    }

}
